package cn.xinyangtong.manager.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * 首页
 * Created by fengbo on 2017/6/8.
 */
@Controller
public class PageController {

    public static final String LOGIN_SESSION = "user_login";
    private static final String CODE_SESSION_KEY = "img_code_cookie";

    @RequestMapping("/")
    public String showIndex() {
        return "index";
    }

    @RequestMapping(value = "/{page}", method = RequestMethod.GET)
    public String showPage(@PathVariable String page) {
        return page;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String code, String username, String password,
                        HttpSession session, Model model) {
        if (StringUtils.isEmpty(code) &&
                !code.equalsIgnoreCase((String) session.getAttribute(CODE_SESSION_KEY))) {
            model.addAttribute("error", "图片验证码错误！");
            return "redirect:/login";
        }
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password) &&
                username.trim().equalsIgnoreCase("admin") &&
                password.trim().equalsIgnoreCase("admin")) {
            session.setAttribute(LOGIN_SESSION, username);
            return "redirect:/index";
        }
        model.addAttribute("error", "账号或密码错误！");
        return "redirect:/login";
    }

    @RequestMapping("/logout")
    public String login(HttpSession session) {
        session.removeAttribute(LOGIN_SESSION);
        return "redirect:/login";
    }

    @RequestMapping("/codeImg")
    public void codeImg(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        int width = 80;
        int height = 32;
        //create the image
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // set the background color
        g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, width, height);
        // draw the border
        g.setColor(Color.black);
        g.drawRect(0, 0, width - 1, height - 1);
        // create a random instance to generate the codes
        Random rdm = new Random();
        String hash1 = Integer.toHexString(rdm.nextInt());
        // make some confusion
        for (int i = 0; i < 50; i++) {
            int x = rdm.nextInt(width);
            int y = rdm.nextInt(height);
            g.drawOval(x, y, 0, 0);
        }
        // generate a random code
        String capstr = hash1.substring(0, 4);
        session.setAttribute(CODE_SESSION_KEY, capstr);
        g.setColor(new Color(0, 100, 0));
        g.setFont(new Font("Candara", Font.BOLD, 24));
        g.drawString(capstr, 8, 24);
        g.dispose();
        response.setContentType("image/jpeg");
//        out.clear();
//        out = pageContext.pushBody();
        OutputStream strm = null;
        try {
            strm = response.getOutputStream();
            ImageIO.write(image, "jpeg", strm);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                strm.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
